package com.delivery.controller.admin;

import com.delivery.dto.CategoryPageQueryDTO;
import com.delivery.dto.UpdateOrAddCategoryDTO;
import com.delivery.entity.Category;
import com.delivery.result.PageResult;
import com.delivery.result.Result;
import com.delivery.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 修改分类
     * @param dto
     * @return
     */
    @PutMapping
    public Result updateCategory(@RequestBody UpdateOrAddCategoryDTO dto) {
        categoryService.updateCategory(dto);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> categoryPageQuery(CategoryPageQueryDTO dto) {
        PageResult pageResult = categoryService.categoryPageQuery(dto);
        return Result.success(pageResult);
    }

    /**
     * 启用、禁用分类
     * @param status 状态 1为启用，0为禁用
     * @param id 分类ID
     * @return
     */
    @PostMapping("/status/{status}")
    public Result enableOrDisableCategory(@PathVariable String status, Long id) {
        categoryService.enableOrDisableCategory(status, id);
        return Result.success();
    }

    /**
     * 新增分类
     * @param dto
     * @return
     */
    @PostMapping
    public Result addCategory(@RequestBody UpdateOrAddCategoryDTO dto) {
        categoryService.addCategory(dto);
        return Result.success();
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteCategory(Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type 分类类型：1为菜品分类，2为套餐分类
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> findCategoriesByType(Integer type) {
        List<Category> categories =  categoryService.findCategoriesByType(type);
        return Result.success(categories);
    }

}
